/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 10, 2011 12:12:26 PM
 */
package carrello.model.entities;

import carrello.model.association.AttributeValue;
import carrello.util.EntityManagerUtil;
import java.util.List;
import java.util.Set;
import javax.persistence.*;

@NamedQueries({
	@NamedQuery(
	    name = "getAllProducts",
	    query = "from Product "
	    ),
        @NamedQuery(
            name = "getProductById",
            query = "from Product p where p.productId = :s"
            ),
        @NamedQuery(
            name = "getExactProductByName",
            query = "from Product p where p.name = :s"
            ),
        @NamedQuery(
            name = "getProductByName",
            query = "from Product p where p.name like  :s "
            )
        })

@Entity
@Table(name = "Product")
public class Product {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)   
    @Column(name = "product_id")
    private int productId;
    @Column(name = "product_name")
    private String name;
    private String description;
    private String price;
    private int    quantity;
    private int    catalogId;
    private String manufacturer;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_Id", nullable = true)
    private Category category;
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pkey.product")
    private Set<AttributeValue> attributeValue;
    

    public Product() {
    }

    public Product(int productId, String name, String description, String price, int quantity, int catalogId, String manufacture, Category category) {
        this.productId = productId;
        this.name = name;
        this.description = description;
        this.price = price;
        this.quantity = quantity;
        this.catalogId = catalogId;
        this.manufacturer = manufacture;
        this.category = category;
    }

    public Product(int productId, String name, String description, String price, int quantity, int catalogId, String manufacture ) {
        this.productId = productId;
        this.name = name;
        this.description = description;
        this.price = price;
        this.quantity = quantity;
        this.catalogId = catalogId;
        this.manufacturer = manufacture;
    }

    public int getCatalogId() {
        return catalogId;
    }

    public void setCatalogId(int catalogId) {
        this.catalogId = catalogId;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacture) {
        this.manufacturer = manufacture;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public int getProduct_id() {
        return productId;
    }

    public void setProduct_id(int productId) {
        this.productId = productId;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Set<AttributeValue> getAttributeValue() {
        return attributeValue;
    }

    public void setAttributeValue(Set<AttributeValue> attributeValue) {
        this.attributeValue = attributeValue;
    }


    
    public static List<Product> getAllProducts() {
       List<Product> list = EntityManagerUtil.getEntityManager().getQuery("getAllProducts").list();

       return list;
       }
    
    
    public static Product getProductById(int productId) {
        Product product = (Product)EntityManagerUtil.getEntityManager().getQuery("getProductById")
                                    .setParameter("s", productId)
                                    .uniqueResult();
        return product;
    }
    
    public static List<Product> getProductByName(String productName) {
        List<Product> productList = EntityManagerUtil.getEntityManager().getQuery("getProductByName")
                                    .setParameter("s", "%" + productName +"%").list();
        return productList;
    }

    public static Product getExactProductByName (String productName) {
        Product p = (Product) EntityManagerUtil.getEntityManager().getQuery("getExactProductByName")
                                    .setParameter("s", productName).uniqueResult();
        return p;
    }
    public static List getAllProductInfoById(int productId) {
        List productInfo = EntityManagerUtil.getHibernateSession().createSQLQuery("select at.name, av.value from"
                + " AttributeValue av, Attribute at where av.attribute_id = at.attribute_id and av.product_id =:s").setParameter("s", productId).list();
        return productInfo;
    }
    
//    public static void main(String[] args)
//    {
//        List list = EntityManagerUtil.getHibernateSession().createSQLQuery("select p.product_name, p.description, p.price, p.quantity, p.catalogId, p.manufacturer, at.name, av.value from Product p, AttributeValue av, Attribute at where p.product_id = av.product_id and av.attribute_id = at.attribute_id and p.product_id = 6").list();
//        System.out.println(Arrays.deepToString(list.toArray()));
//    }
    }

